<template>
  <div class="energy-management padding">
    <div class="left-panel panel dark-table-wrapper">
      <div class="panel-header">
        <h2>能源大类</h2>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="showAddClassifyModal"
        >
          新增
        </el-button>
      </div>
      <div class="panel-body">
        <el-table
          ref="leftTable"

          height="100%"
          :header-cell-style="{
            backgroundColor: themeColor,
            color: 'white'
          }"
          highlight-current-row
          :data="typeList"
          style="width: 100%"
          @row-click="handleRowClick"
        >
          <el-table-column prop="num" label="序号" width="80"></el-table-column>
          <el-table-column prop="name" label="名称"></el-table-column>
          <el-table-column prop="energyType" label="编码"></el-table-column>
          <el-table-column prop="unit" label="基础单位"></el-table-column>
          <el-table-column prop="ratio" label="转换系数"></el-table-column>
          <el-table-column label="操作" fixed="right" width="100">
            <template #default="{ row }">
              <td-button title="编辑" @click="showEditClassifyModal(row)">
                <i class="i-custom:editor"></i>
              </td-button>
              <td-button title="删除"
                class="del-btn"
                icon-style="color: var(--danger-color);border-color: var(--danger-color);"
                @click="delClassify(row)"
              >
                <i class="i-custom:ashbin"></i>
              </td-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div ref="fakeGap" class="right-panel panel dark-table-wrapper">
      <div class="panel-header">
        <h2>能源种类</h2>
        <el-button type="primary" icon="el-icon-plus" @click="showAddInfoModal">
          新增
        </el-button>
      </div>
      <div class="panel-body">
        <el-table

          height="100%"
          :header-cell-style="{
            backgroundColor: themeColor,
            color: 'white'
          }"
          :data="typeKindList"
          style="width: 100%"
        >
          <el-table-column prop="num" label="序号" width="80"></el-table-column>
          <el-table-column prop="energyName" label="名称"></el-table-column>
          <el-table-column prop="energyNumber" label="编码"></el-table-column>
          <el-table-column prop="energyUnit" label="单位"></el-table-column>
          <el-table-column label="操作" fixed="right" width="100">
            <template #default="{ row }">
              <td-button
                title="编辑"
                @click="showEditInfoModal(row)"
              >
                <i class="i-custom:editor"></i>
              </td-button>
              <td-button
                title="删除"
                class="del-btn"
                icon-style="color: var(--danger-color);border-color: var(--danger-color);"
                @click="delInfo(row)"
              >
                <i class="i-custom:ashbin"></i>
              </td-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <ClassifyModal
      ref="classifyModalRef"
      :modalTitle="classifyModalTitle"
      :data="typeFormData"
      :index="typeList.length"
      @save-success="getTypeList"
    />
    <InfoModal
      ref="infoModalRef"
      :modalTitle="infoModalTitle"
      :data="infoFormData"
      :parentInfo="selectTypeInfo"
      :energyCode="energyCode"
      :index="typeKindList.length"
      :existTypeKind="existTypeKind"
      @save-success="() => getTypeKindList()"
    />
  </div>
</template>

<script>
import TdButton from '@/components/td-button';
import ClassifyModal from './components/classify-modal.vue';
import InfoModal from './components/info-modal.vue';
import * as api from '@/api/energy';
import { confirmDel } from '@/utils';
import { mapMutations } from 'vuex';

import mixinFakeGap from '@/mixins/mixin-fake-gap';

export default {
  name: 'energy-management',
  mixins: [mixinFakeGap],
  components: {
    TdButton,
    ClassifyModal,
    InfoModal
  },
  data: () => ({
    classifyModalTitle: '新增能源大类',
    infoModalTitle: '新增能源子类',
    typeList: [],
    typeKindList: [],
    typeFormData: {},
    infoFormData: {},
    selectTypeInfo: {},
    energyCode: []
  }),
  created() {
    this.getTypeList();
    this.getEnergyCode();
  },
  updated () {
    if (this.$refs.fakeGap) {
      this.fixFakeGap('marginLeft', this.$refs.fakeGap)
    }
  },
  computed: {
    themeColor() {
      return this.$store.state.user.theme;
    },
    existTypeKind() {
      const o = this.typeKindList.map(item => item.code);
      return o;
    }
  },
  methods: {
    ...mapMutations(['updateEnergyType']),
    showAddClassifyModal() {
      this.typeFormData = {};

      this.$nextTick(() => {
        this.$refs.classifyModalRef.open();
      });
    },
    showEditClassifyModal(row) {
      this.typeFormData = { ...row };
      this.$nextTick(() => {
        this.$refs.classifyModalRef.open();
      });
    },
    delClassify(row) {
      confirmDel(this)
        .then(() => {
          api.deleteTypeById(row.id).then(res => {
            if (res.code === 200) {
              this.getTypeList();
              this.$message.success('删除成功');
            }
          });
        })
        .catch(() => {});
    },
    showAddInfoModal() {
      this.infoFormData = {};
      this.$nextTick(() => {
        this.$refs.infoModalRef.open();
      });
    },
    showEditInfoModal(row) {
      this.infoFormData = { ...row };
      this.$nextTick(() => {
        this.$refs.infoModalRef.open();
      });
    },
    delInfo(row) {
      confirmDel(this)
        .then(() => {
          api.deleteKindById(row.id).then(res => {
            if (res.code === 200) {
              this.getTypeList();
              this.$message.success('删除成功');
            }
          });
        })
        .catch(() => {});
    },
    async getTypeList() {
      const { data: res, code } = await api.getTypeList();
      if (code !== 200) {
        this.typeList = [];
        this.updateEnergyType([]);
        return;
      }
      this.typeList = res;
      this.updateEnergyType(res);
      if (res.length === 0) return;
      this.$refs.leftTable.setCurrentRow(res[0]);
      this.getTypeKindList(res[0].energyType);
      this.selectTypeInfo = res[0];
    },
    async getTypeKindList(type = this.selectTypeInfo.energyType) {
      const { data: res, code } = await api.getTypeKindList(type);
      if (code !== 200) {
        this.typeKindList = [];
        return;
      }
      this.typeKindList = res;
    },
    handleRowClick(row) {
      this.selectTypeInfo = row;
      this.getTypeKindList(row.energyType);
    },
    async getEnergyCode() {
      const { data: res, code } = await api.getEnergyCode();
      if (code !== 200) {
        this.energyCode = [];
        return;
      }
      this.energyCode = res;
    }
  }
};
</script>

<style scoped lang="scss">
.energy-management {
  display: flex;
  height: 100%;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;
  gap: 20px;

  .left-panel {
    flex: 1;
  }
  .right-panel {
    flex: 1;
  }
  .panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
    &-header {
      display: flex;
      align-items: center;
      flex: 0 0 auto;
      h2 {
        margin: 0px;
        margin-right: 20px;
      }
      margin-bottom: 20px;
    }
    &-body {
      overflow: hidden;
      flex: 1 1 auto;
    }
  }
}
</style>
